package com.nulldev.util.java.swing.extSwing;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.nulldev.util.java.swing.SwingUtil;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.ThemeApplier;
import com.nulldev.util.java.swing.font.FontManager;
import com.nulldev.util.math.MathUtil;
import com.nulldev.util.other.ArrayUtils;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JList;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import javax.swing.JCheckBox;
import java.awt.Toolkit;

public class eFontPicker extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					eFontPicker frame = new eFontPicker();
					ThemeApplier.applyTheme(frame);
					SwingUtil.centerFrame(frame);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private final FontListCellRenderer flcr = new FontListCellRenderer();
	private JList list;
	
	/**
	 * Create the frame.
	 */
	public eFontPicker() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(eFontPicker.class.getResource("/com/nulldev/util/java/swing/nullutil_logo.png")));
		setTitle("extSwing.eFontPicker");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 434, 416);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblSelectAFont = new JLabel("Select a font.");
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel lblFontSize = new JLabel("Font Size | 16");
		
		final JSlider slider = new JSlider();
		slider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				flcr.setFontSize((int) slider.getValue());
				lblFontSize.setText("Font Size | " + slider.getValue());
				if (list != null) {
					list.invalidate();
					list.repaint();
				}
			}
		});
		slider.setValue(16);
		slider.setMinimum(8);
		slider.setMaximum(32);
		
		final JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final String fontSelected = (String) list.getSelectedValue();
				final int fontSize = MathUtil.clamp(8, 32, slider.getValue());
				setVisible(false);
				dispose();
				FontManager.setFont(FontManager.getFont(fontSelected), fontSize);
				FontManager.applyAll(FontManager.getFontSet());
			}
		});
		
		final JCheckBox chckbxDisplayExample = new JCheckBox("Display example");
		chckbxDisplayExample.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				flcr.setTextDemo(chckbxDisplayExample.isSelected());
				if (list != null) {
					list.invalidate();
					list.repaint();
				}
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
						.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
							.addComponent(chckbxDisplayExample, GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE)
							.addGap(8)
							.addComponent(btnOk))
						.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
							.addComponent(lblFontSize)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(slider, GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE))
						.addComponent(lblSelectAFont))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(lblSelectAFont)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
					.addGap(7)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblFontSize)
						.addComponent(slider, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnOk)
						.addComponent(chckbxDisplayExample)))
		);
		
		final ArrayList<String> llistc = FontManager.getGlobalFonts();
		final String fn = FontManager.getFontSet().getName()
				.replaceAll("MADEEvolveSans", "MADE Evolve Sans")
				.replaceAll("Traffic 02", "Traffic");
		final int pos = ArrayUtils.get(llistc, fn);
		list = new JList(ArrayUtils.arrayListToArray(FontManager.getGlobalFonts()));
		list.setSelectedIndex(pos);
		flcr.setFontSize(16);
		list.setCellRenderer(flcr);
		scrollPane.setViewportView(list);
		contentPane.setLayout(gl_contentPane);
	}
	
	protected JList getFontList() {
		return list;
	}
}
